import ACCESS_ENUM from "@/access/accessEnum";

const checkAccess: (
  loginUser: any,
  needAccess?: string | string[]
) => boolean | boolean = (
  loginUser: any,
  needAccess: string | string[] = ACCESS_ENUM.NOT_LOGIN
) => {
  const loginUserAccess = loginUser?.userRole ?? ACCESS_ENUM.NOT_LOGIN;
  if (needAccess === ACCESS_ENUM.NOT_LOGIN) {
    return true;
  }
  if (needAccess === ACCESS_ENUM.USER) {
    return loginUserAccess !== ACCESS_ENUM.NOT_LOGIN;
  }
  if (needAccess === ACCESS_ENUM.ADMIN) {
    return loginUserAccess === ACCESS_ENUM.ADMIN;
  }
  // 处理数组形式的权限需求
  if (Array.isArray(needAccess)) {
    // 如果需要的权限为空，则所有用户都可以访问
    if (needAccess.length === 0) {
      return true;
    }
    // 只要满足其中一个权限即可
    return needAccess.some((access) => checkAccess(loginUser, access));
  }

  return true;
};
export default checkAccess;
